import Router from "vue-router"
import Vue from "vue"
import store from "../store"

// import Login from "@/components/Login.vue"
// import Home from "@/components/Home.vue"
// import Welcome from "@/components/Welcome.vue"
// import Users from "@/components/user/Users.vue"
// import Permission from "@/components/power/Permission.vue"
// import Roles from "@/components/power/Roles.vue"
// import Cates from "@/components/goods/Cate.vue"
// import Params from "../components/goods/Params.vue"
// import GoodsList from "../components/goods/List.vue"
// import AddGoods from "../components/goods/Add.vue"
// import Order from "../components/order/Order.vue"
// import Reports from "../components/report/Report.vue"

// 路由懒加载
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue')

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users.vue')
const Permission = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Permission.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

const Cates = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')

const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/List.vue')
const AddGoods = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')

const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')
const Reports = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')

const Error = () => import(/* webpackChunkName: "Error" */ '../components/404.vue')



// 注册路由
Vue.use(Router);

// 配置路由
const router = new Router({
    // mode: 'history',
    routes: [
        { path: "/", redirect: "/home"},
        { path: "/Login", component: Login},
        { 
            path: "/home", 
            component: Home,
            redirect: "/welcome", 
            children: [
               {path: "/welcome", component: Welcome},
               {path: "/users", component: Users, meta: {requireAuth: true}},
               {path: "/rights", component: Permission, meta: {requireAuth: true}},
               {path: "/roles", component: Roles, meta: {requireAuth: true}},
               {path: "/categories", component: Cates, meta: {requireAuth: true}},
               {path: "/params", component: Params, meta: {requireAuth: true}},
               {path: "/goods", component: GoodsList, meta: {requireAuth: true}},
               {path: "/goods/add", component: AddGoods, meta: {requireAuth: true}},
               {path: "/orders", component: Order, meta: {requireAuth: true}},
               {path: "/reports", component: Reports, meta: {requireAuth: true}},
               {path: "/404", component: Error},
            ]
        },
        { path: "*", redirect: "/404"},
    ]
});

router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    //ffrom 从哪里而来
    // next 函数，next()放行  next("/login") 强制跳转
    if (to.path == "/login") return next()
    if (to.meta.requireAuth){
        if (!store.getters.api_path_map.hasOwnProperty(to.path)){
            return next('/404')
        }
    }
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next("/login")
    next()
})

export default router